package com.github.yealove.configgen.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.yealove.configgen.exception.Assert;
import com.github.yealove.configgen.mapper.ResultMapper;
import com.github.yealove.configgen.model.*;
import com.github.yealove.configgen.service.ConfigService;
import com.github.yealove.configgen.service.GroupService;
import com.github.yealove.configgen.service.ResultService;
import com.github.yealove.configgen.service.TemplateService;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ResultServiceImpl implements ResultService {

    @Autowired
    private ConfigService configService;

    @Autowired
    private GroupService groupService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private ResultMapper resultMapper;

    @Override
    public void genResult(int tId, int gId) {
        genResult(CollectionUtil.toList(tId), gId);
    }

    @Override
    public void genResult(List<Integer> templateIds, int gId) {
        List<Config> configs = configService.getByGid(gId);
        Assert.assertNotEmpty(configs, "配置参数为空");

        Map<String, String> configMap = configs.stream().collect(Collectors.toMap(Config::getCKey, Config::getCValue));
        StringSubstitutor sub = new StringSubstitutor(configMap, ConfigGenConstant.prefix, ConfigGenConstant.suffix);
        for (Integer tId : templateIds) {
            Template t = templateService.getTemplateById(tId);
            if (t == null) {
                continue;
            }
            String resultContent = sub.replace(t.getContent());

            Group group = groupService.getGroupById(gId);
            Result result = Result.builder()
                    .fileName(t.getName().replace("{}", group.getName()))
                    .fileContent(resultContent)
                    .fileType(t.getFileType())
                    .nacosGroup(t.getNacosGroup())
                    .nacosNamespace(t.getNacosNamespace())
                    .version(t.getVersion())
                    .build();
            resultMapper.addResult(result);
        }
    }

    @Override
    public int saveResult(Result result) {
        return resultMapper.addResult(result);
    }

    @Override
    public List<Result> searchResult(String search, String startTime, String endTime) {
        if (StringUtils.isBlank(startTime)) {
            startTime = "2021-01-01 00:00:00";
        }
        if (StringUtils.isBlank(endTime)) {
            endTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        return resultMapper.searchResult(search, startTime, endTime);
    }

    @Override
    public List<Result> getResultByIds(List<Integer> rIds) {
        return resultMapper.getResultByIds(rIds);
    }

    @Override
    public void clear() {
        resultMapper.removeAll();
    }

    @Override
    public void addPushLog(NacosItem nacosItem) {
        resultMapper.addPushLog(nacosItem);
    }
}
